﻿@page "/movies"
@rendermode InteractiveServer
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.QuickGrid
@using BlazorWebAppMovies.Models
@using BlazorWebAppMovies.Data
@implements IAsyncDisposable
@inject IDbContextFactory<BlazorWebAppMovies.Data.BlazorWebAppMoviesContext> DbFactory

<PageTitle>Index</PageTitle>

<h1>Index</h1>

<p>
    <a href="movies/create">Create New</a>
</p>
<div>
    @* <form action="/movies" data-enhance data-permanent>
        <input type="search" name="titleFilter" />
        <input type="submit" value="Search" />
    </form> *@
    <input type="search" @bind="titleFilter" @bind:event="oninput" />
</div>
<div>
    <QuickGrid Class="table" Items="FilteredMovies" Pagination="pagination">
        <PropertyColumn Property="movie => movie.Title" Sortable="true" />
        <PropertyColumn Property="movie => movie.ReleaseDate" Title="Release date" />
        <PropertyColumn Property="movie => movie.Genre" />
        <PropertyColumn Property="movie => movie.Price" />
        <PropertyColumn Property="movie => movie.Rating" />
        <TemplateColumn Context="movie">
            <a href="@($"movies/edit?id={movie.Id}")">Edit</a> |
            <a href="@($"movies/details?id={movie.Id}")">Details</a> |
            <a href="@($"movies/delete?id={movie.Id}")">Delete</a>
        </TemplateColumn>
    </QuickGrid>
</div>
<Paginator State="pagination" />
@code {

    private PaginationState pagination = new PaginationState { ItemsPerPage = 5 };

    // [SupplyParameterFromQuery]
    // private string? TitleFilter { get; set; } = default!;

    // private IQueryable<Movie> FilteredMovies =>
    // context.Movie.Where(m => m.Title!.Contains(TitleFilter ?? string.Empty));

    private string titleFilter = string.Empty;
    private IQueryable<Movie> FilteredMovies => context.Movie.Where(m => m.Title!.Contains(titleFilter));

    private BlazorWebAppMoviesContext context = default!;



    protected override void OnInitialized()
    {
        context = DbFactory.CreateDbContext();
    }

    public async ValueTask DisposeAsync() => await context.DisposeAsync();
}
